<?php
/**
 * Created by PhpStorm.
 * User: aiChenK
 * Date: 2018-12-18
 * Time: 10:11
 */
namespace Admin\Frontend\Controllers;

use Helpers\File;
use MongoDB\Driver\Exception\InvalidArgumentException;

class FileController extends ControllerBase
{

    /**
     * mongo文件下载
     *
     * @param string $module
     * @param string $subModule
     * @param string $target
     * @return bool|\Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     *
     * create by ck 20181218
     */
    public function downloadAction($module = '', $subModule = '', $target = '')
    {
        if (!$module || !$subModule || !$target) {
            return $this->sendErrorResponse(403, '参数有误，拒绝下载请求');
        }
        $fileId = explode('.', $target)[0];
        try {
            return File::download($module, $subModule, $fileId);
        } catch (InvalidArgumentException $e) {
            return $this->sendErrorResponse(404, '文件不存在', $e->getMessage());
        } catch (\Exception $e) {
            return $this->sendErrorResponse(500, '系统异常', $e->getMessage());
        }
    }
}
